package cn.chenfei.jfx.login;

import cn.chenfei.jfx.App;
import cn.chenfei.jfx.common.JFUtils;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.javafx.FontIcon;


/**
 * @author ChenFei
 * @date 2022/4/10
 */
public class Login_3 extends StackPane {

    private ObservableList<Node> children = this.getChildren();

    private VBox container = new VBox();
    private HBox topBox = new HBox();

    {
        this.setPadding(new Insets(10, 10, 10, 10));
        this.setBackground(null);
        //
        children.add(container);
        children.add(topBox);
        container.getStyleClass().add("container");
        container.setAlignment(Pos.CENTER);
        this.setAlignment(topBox, Pos.TOP_RIGHT);
        topBox.setMaxWidth(0);
        topBox.setMaxHeight(0);
        topBox.getStyleClass().add("top-box");
        this.getStylesheets().add(App.class.getResource("css/Login_3.css").toExternalForm());
        //拖动
        JFUtils.dragCurrentWindow(container);
    }

    private Button hideBut = new Button();
    private Button closeBut = new Button();

    {
        topBox.getChildren().addAll(hideBut, closeBut);
        hideBut.setGraphic(new FontIcon(AntDesignIconsOutlined.MINUS));
        closeBut.setGraphic(new FontIcon(AntDesignIconsOutlined.CLOSE));
        hideBut.getStyleClass().add("hide-but");
        closeBut.getStyleClass().add("close-but");
        hideBut.setOnMouseClicked(event -> JFUtils.setIconified(event.getSource(),true));
        closeBut.setOnMouseClicked(event -> JFUtils.closeCurrentWindow(event.getSource()));
    }

    private Label titleLabel = new Label("请选择你的身份");

    {
        container.getChildren().add(titleLabel);
        titleLabel.getStyleClass().add("title-label");
    }

    /**
     * 账户类型布局-----------------------------------------------------------
     */
    private HBox accountTypeBox = new HBox();
    private AnchorPane adminAp = new AnchorPane();
    private AnchorPane civilianAp = new AnchorPane();
    private VBox adminBox = new VBox();
    private VBox civilianBox = new VBox();
    //
    private Label adminCheckLabel = new Label();
    private Label civilianCheckLabel = new Label();

    {
        adminCheckLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.CHECK));
        civilianCheckLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.CHECK));
        //账户类型布局
        adminAp.getChildren().addAll(adminBox, adminCheckLabel);
        adminAp.setRightAnchor(adminCheckLabel, -10.0);
        adminAp.setBottomAnchor(adminCheckLabel, -10.0);
        civilianAp.getChildren().addAll(civilianBox, civilianCheckLabel);
        civilianAp.setRightAnchor(civilianCheckLabel, -10.0);
        civilianAp.setBottomAnchor(civilianCheckLabel, -10.0);
        container.getChildren().add(accountTypeBox);
        accountTypeBox.getChildren().addAll(adminAp, civilianAp);
        accountTypeBox.getStyleClass().add("account-type-box");
        adminBox.getStyleClass().add("admin-box");
        civilianBox.getStyleClass().add("civilian-box");
        adminCheckLabel.getStyleClass().add("admin-check-label");
        civilianCheckLabel.getStyleClass().add("civilian-check-label");
    }

    private Pane adminImg = new Pane();
    private Pane userImg = new Pane();
    private Label adminLabel = new Label("管理员");
    private Label civilianLabel = new Label("普通用户");

    {
        adminBox.getChildren().addAll(adminImg, adminLabel);
        civilianBox.getChildren().addAll(userImg, civilianLabel);
        adminImg.getStyleClass().add("admin-img");
        adminLabel.getStyleClass().add("admin-label");
        userImg.getStyleClass().add("civilian-img");
        civilianLabel.getStyleClass().add("civilian-label");
    }

    private Label prompt = new Label("这里只是一段提示文字。");

    {
        container.getChildren().addAll(prompt);
        prompt.getStyleClass().add("prompt-label");
    }

    /**
     * 输入框布局-----------------------------------------------------------
     */
    private HBox userBox = new HBox();
    private Label userLabel = new Label();
    private TextField userField = new TextField();
    //
    private HBox pswBox = new HBox();
    private Label pswLabel = new Label();
    private PasswordField pswField = new PasswordField();
    private Separator separator = new Separator(Orientation.VERTICAL);
    private Button forget = new Button("忘了？");

    {

        container.getChildren().addAll(userBox, pswBox);
        //
        userBox.getChildren().addAll(userLabel, userField);
        userBox.setHgrow(userField, Priority.ALWAYS);
        userBox.getStyleClass().add("user-box");
        userLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.MAIL));
        userLabel.getStyleClass().add("user-label");
        userField.setPromptText("邮箱");
        userField.getStyleClass().add("user-field");
        //
        pswBox.getChildren().addAll(pswLabel, pswField, separator, forget);
        userBox.setHgrow(pswField, Priority.ALWAYS);
        pswBox.getStyleClass().add("psw-box");
        pswLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.KEY));
        pswLabel.getStyleClass().add("psw-label");
        pswField.getStyleClass().add("psw-field");
        forget.getStyleClass().add("forget-but");
        pswField.setPromptText("密码");
    }

    /**
     * 底部按钮布局-------------------------------------------------------------------
     */
    private StackPane bottomSp = new StackPane();
    private Button register = new Button("没有账户？注册一个。");
    private Button login = new Button("登录");

    {
        container.getChildren().add(bottomSp);
        bottomSp.getChildren().addAll(register, login);
        bottomSp.setAlignment(register, Pos.CENTER_LEFT);
        bottomSp.setAlignment(login, Pos.TOP_RIGHT);
        bottomSp.getStyleClass().add("bottom-sp");
        register.getStyleClass().add("register-but");
        login.getStyleClass().add("login-but");
    }

    //这里记录一个复选框，用来监听用户类型
    private CheckBox checkBox = new CheckBox();
    private Label message = new Label();

    {
        container.getChildren().add(message);
        message.getStyleClass().add("message");
    }

    //显示加载层
    StackPane loading = new StackPane();

    {
        //加载动画
        ProgressIndicator indicator = new ProgressIndicator();
        loading.setStyle("-fx-background-color: rgba(0, 0, 0, 0.5);");
        indicator.setStyle("-fx-progress-color:cf-blue;");
        loading.getChildren().add(indicator);
        children.add(loading);
        loading.setVisible(false);
    }


    public Login_3() {
        //输入框选中事件绑定
        userField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "cf-blue;" : "cf-thin-blue;";
            userBox.setStyle(String.format("-fx-border-color:%s", color));
            userLabel.setStyle(String.format("-fx-border-color:%s", color));
            userLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
        });
        pswField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "cf-blue;" : "cf-thin-blue;";
            pswBox.setStyle(String.format("-fx-border-color:%s", color));
            pswLabel.setStyle(String.format("-fx-border-color:%s", color));
            pswLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
        });
        //角色事件切换监听
        checkBox.selectedProperty().addListener((observableValue, aBoolean, t1) -> {
            if (t1) {
                //选中了管理员
                adminCheckLabel.setStyle("-fx-background-color:cf-blue;-fx-effect: dropshadow(three-pass-box, cf-blue, 10.0, 0, 0, 0);");
                civilianCheckLabel.setStyle("-fx-background-color:#E4E7ED;");
            } else {
                //选中了普通用户
                civilianCheckLabel.setStyle("-fx-background-color:cf-blue;-fx-effect: dropshadow(three-pass-box, cf-blue, 10.0, 0, 0, 0);");
                adminCheckLabel.setStyle("-fx-background-color:#E4E7ED;");

            }
        });
        //角色切换事件
        adminBox.setOnMouseClicked(event -> checkBox.setSelected(true));
        civilianBox.setOnMouseClicked(event -> checkBox.setSelected(false));
        adminCheckLabel.setOnMouseClicked(event -> checkBox.setSelected(true));
        civilianCheckLabel.setOnMouseClicked(event -> checkBox.setSelected(false));
        //默认选中管理员
        checkBox.setSelected(true);
        //登录点击事件
        login.setOnMouseClicked(event -> {
            event.consume();
            String username = userField.getText();
            String password = pswField.getText();
            System.out.println(String.format("username: %s ; password: %s ; 角色 : %s ;", username, password, checkBox.isSelected() ? "管理员" : "普通用户"));
            if (username.trim().equals("")) {
                message.setText("邮箱不能为空");
                return;
            }
            if (password.trim().equals("")) {
                message.setText("密码不能为空");
                return;
            }
            message.setText("");
            loading.setVisible(true);
            //3秒后关闭
            new Thread(() -> {
                try {
                    Thread.sleep(3000);
                    Platform.runLater(() -> loading.setVisible(false));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();

        });
        //
        EventHandler eventUrl = (EventHandler<Event>) e -> new App().getHostServices().showDocument("https://openjfx.cn/");
        register.setOnMouseClicked(eventUrl);
        forget.setOnMouseClicked(eventUrl);
    }


}
